<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class FichePage {
	
	private $db_access;
	private $item_functions;

	function __construct() {
		$this->db_access = new DbAccessor();
		$this->item_functions = new ItemFunctions();
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		$this->item_functions->item_page_inc_check_access($page);
	}

	/**
	 * appropriate page action interpretations
	 */
	function page_inc_execute_action($action) {
		switch($action) {
			default:
				$this->item_functions->item_page_inc_execute_action($action, 'fiche');
				break;
		}
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {
		$CONTENT = array();

		$where = "ITEM.item_id = {$page->id} AND
				  ITEM.item_id = GAME_FICHE.item_id AND 
				  GAME_FICHE.game_id = GAME.game_id AND 
				  GAME_EDITOR.game_editor_id = GAME_FICHE.game_editor_id AND 
				  ITEM.author = M1.user_id AND 
				  ITEM.editor = M2.user_id";

		$sql = $this->db_access->db_query_select(array(array('*'),array('*'),array('*'),array('*'),array(),"M1.nick AS nick_author, M2.nick AS nick_editor"), 
									  array('ITEM','GAME_FICHE','GAME','GAME_EDITOR','MEMBER AS M1, MEMBER AS M2', 'DUMMY'), $where);
		$sqlresult = $this->db_access->db_do_query($sql);

		if ($this->db_access->db_num_rows($sqlresult) !=1) {
			errorpage(404);
		}

		$CONTENT['fiche'] = $this->db_access->db_fetch_array($sqlresult);

		// fetch path to forum
		$url = "";
		$name = "";
		$dir = $CONTENT['fiche']['forum_link'];
		while ($dir > 0) {
			$url = "/" . $page->menu[$dir]['name'] . $url;
			$name = " > " . $page->menu[$dir]['menuname'] . $name;
			$dir = $page->menu[$dir]['parent_id'];
		}
		
		$CONTENT['fiche']['forum_link_url'] = $url;
		$CONTENT['fiche']['forum_link_name'] = ereg_replace("^ > ", "", $name);

		if ($CONTENT['fiche']['deleted']) {
			errorpage(404);
		}
		
		$CONTENT['title'] = $CONTENT['fiche']['title'];
		$CONTENT['owner'] = $CONTENT['fiche']['author'];
			
		$page->content = $CONTENT;
		
		$this->item_functions->item_page_inc_execute($page, 'fiche');
	}


	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		$this->item_functions->item_page_inc_dress_before($page);
        $page->content['fiche']['image_link'] = $this->db_access->db_desqlize($page->content['fiche']['image_link']);
        $page->content['fiche']['description'] = $this->db_access->db_desqlize($page->content['fiche']['description']);
        $page->content['fiche']['genre'] = $this->db_access->db_desqlize($page->content['fiche']['genre']);
        $page->content['fiche']['players'] = $this->db_access->db_desqlize($page->content['fiche']['players']);
        $page->content['fiche']['rules'] = $this->db_access->db_desqlize($page->content['fiche']['rules']);
        $page->content['fiche']['requirements'] = $this->db_access->db_desqlize($page->content['fiche']['requirements']);
        $page->content['fiche']['expansions'] = $this->db_access->db_desqlize($page->content['fiche']['expansions']);
        $page->content['fiche']['did_you_know'] = $this->db_access->db_desqlize($page->content['fiche']['did_you_know']);
        $page->content['fiche']['price'] = $this->db_access->db_desqlize($page->content['fiche']['price']);
        $page->content['fiche']['languages'] = $this->db_access->db_desqlize($page->content['fiche']['languages']);
		print_fiche($page, $page->content['fiche']);
		$this->item_functions->item_page_inc_dress_after($page);
	}
}

?>
